package com.simon.jkxj.ui.consultation

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.simon.jkxj.R
import com.simon.jkxj.databinding.ActivityDoctorListBinding
import de.hdodenhof.circleimageview.CircleImageView

class DoctorListActivity : AppCompatActivity() {

    private lateinit var binding: ActivityDoctorListBinding
    private val doctors = listOf(
        mapOf("id" to "d001", "name" to "张医生", "specialty" to "呼吸内科", "isOnline" to true, "avatar" to "doctor_zhang", "introduction" to "张医生拥有15年呼吸内科临床经验，擅长慢性支气管炎、哮喘等疾病的诊治。"),
        mapOf("id" to "d002", "name" to "李医生", "specialty" to "中医内科", "isOnline" to false, "avatar" to "doctor_li", "introduction" to "李医生是资深中医专家，对中医调理、针灸推拿有独到见解，深受患者好评。"),
        mapOf("id" to "d003", "name" to "王医生", "specialty" to "儿科", "isOnline" to true, "avatar" to "doctor_wang", "introduction" to "王医生是儿科主任医师，专注于儿童呼吸道疾病和常见病的治疗。")
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDoctorListBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupToolbar()
        setupDoctorList()
    }

    private fun setupToolbar() {
        binding.toolbar.title = "在线问诊"
        binding.toolbar.setNavigationOnClickListener {
            onBackPressed()
        }
    }

    private fun setupDoctorList() {
        binding.recyclerViewDoctors.layoutManager = LinearLayoutManager(this)
        binding.recyclerViewDoctors.adapter = DoctorAdapter(doctors) { doctorMap ->
            val intent = Intent(this, ChatActivity::class.java).apply {
                putExtra("doctorId", doctorMap["id"] as String)
                putExtra("doctorName", doctorMap["name"] as String)
                putExtra("doctorAvatar", doctorMap["avatar"] as String)
            }
            startActivity(intent)
        }
    }
}

class DoctorAdapter(private val doctors: List<Map<String, Any>>, private val onItemClick: (Map<String, Any>) -> Unit) :
    RecyclerView.Adapter<DoctorAdapter.DoctorViewHolder>() {

    inner class DoctorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val avatar: CircleImageView = itemView.findViewById(R.id.iv_doctor_avatar)
        val name: TextView = itemView.findViewById(R.id.tv_doctor_name)
        val specialty: TextView = itemView.findViewById(R.id.tv_doctor_specialty)
        val status: TextView = itemView.findViewById(R.id.tv_doctor_status)
        val introduction: TextView = itemView.findViewById(R.id.tv_doctor_introduction)

        fun bind(doctor: Map<String, Any>) {
            name.text = doctor["name"] as String
            specialty.text = doctor["specialty"] as String
            val isOnline = doctor["isOnline"] as Boolean
            status.text = if (isOnline) "在线" else "离线"
            status.setBackgroundResource(if (isOnline) R.drawable.bg_online_status else R.drawable.bg_offline_status)
            introduction.text = doctor["introduction"] as String

            val avatarName = doctor["avatar"] as String
            val resId = itemView.context.resources.getIdentifier(
                avatarName, "drawable", itemView.context.packageName)
            if (resId != 0) {
                avatar.setImageResource(resId)
            } else {
                avatar.setImageResource(R.drawable.me) // fallback placeholder
            }

            itemView.setOnClickListener { onItemClick(doctor) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DoctorViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_doctor, parent, false)
        return DoctorViewHolder(view)
    }

    override fun onBindViewHolder(holder: DoctorViewHolder, position: Int) {
        holder.bind(doctors[position])
    }

    override fun getItemCount() = doctors.size
}